package com.hcbd.tdwt.ui.xingyu.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.hcbd.tdwt.base.GlobalParams;
import com.hcbd.tdwt.ui.xingyu.service.LocationReportService;
import com.hcbd.tdwt.ui.xingyu.vo.LocationTimer;
import com.hcbd.tdwt.util.SharedPreferencesUtil;
import com.hcbd.tdwt.view.WheelView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.fragment_app_location_up)
public class AppLocationUpFragment extends BaseFragment {
    private String TAG = "AppLocationUpFragment";
    @ViewById
    WheelView selectTimeWheel;
    @ViewById
    TextView startBtn,frequencyTV,nextSendTipsTV;
    @ViewById
    LinearLayout customFreGroup;
    @ViewById
    EditText customFre;

    public int currentIndex=1;
    Drawable ovaBlueDrawable;
    Drawable ovaGrayDrawable;
    Drawable ovaRedDrawable;
    private String text="当前北斗卡服务频度为";
    private String currentItem="自定义上报频度";
    private final HashMap<String,Integer>hashMap=new HashMap<>();
    private final String SELECT_FRQ_KEY="select_frq_key";
    SharedPreferencesUtil sp;
    String locationServicePath="com.hcbd.tdwt.ui.xingyu.service.LocationReportService";
    @AfterViews
    protected void init() {
        ovaBlueDrawable=getResources().getDrawable(R.drawable.ova_light_blue_bg);
        ovaRedDrawable=getResources().getDrawable(R.drawable.ova_red_bg2);
        ovaGrayDrawable=getResources().getDrawable(R.drawable.ova_gray_bg);
        sp=new SharedPreferencesUtil(Objects.requireNonNull(this.getActivity()));
        initHashMap();
        frequencyTV.setText(MessageFormat.format("{0}{1}秒", text, BeidouBoxParams.sendFreq));
        initView();
        if(isServiceWork(locationServicePath)){
            setStartBtn(ovaRedDrawable,"暂停");
            selectTimeWheel.isTonch=false;
        }else{
            selectTimeWheel.isTonch=true;
            if(BeidouBoxParams.isBoxConnectNormal){
                setStartBtn(ovaBlueDrawable,"启动");
            }else{
                setStartBtn(ovaGrayDrawable,"未连接");
            }
        }
        initMsgRegister();
    }


    @Click
    void startBtn(){
        if(isServiceWork(locationServicePath)){
            showOldProtocolDialog("当前上报服务正在运行,是否关闭？", (dialog, confirm) -> {
                if(confirm){
                    AppLocationUpFragment.this.getContext().stopService(new Intent(AppLocationUpFragment.this.getContext(), LocationReportService.class));
                    setStartBtn(ovaBlueDrawable,"启动");
                    sp.addAttribute(SELECT_FRQ_KEY,0);
                    selectTimeWheel.isTonch=true;
                    dialog.dismiss();
                }
            });
            return;
        }
        if (!BeidouBoxParams.isBoxConnectNormal) {
            toast("使用此功能请先开启北斗服务");
            return;
        }
        if(GlobalParams.positions.size()==0){
            toast("当前无定位信息,无法上报!");
            return;
        }
        if(currentItem.equals("自定义上报频度")){
            if(customFre.getText().toString().equals("")){
                toast("请输入自定义频度!");
                return;
            }
            int custom_fre = Integer.parseInt(customFre.getText().toString());
            GlobalParams.locationReportFrequency=custom_fre;
        } else {
            GlobalParams.locationReportFrequency=hashMap.get(currentItem);
        }
        Intent mIntent = new Intent(AppLocationUpFragment.this.getContext(), LocationReportService.class);
        getContext().startService(mIntent);
        setStartBtn(ovaRedDrawable,"暂停");
        sp.addAttribute(SELECT_FRQ_KEY,currentIndex);
        selectTimeWheel.isTonch=false;
        toast("已启动!");
    }
    private void initView() {
        int key=sp.getAttributeInt(SELECT_FRQ_KEY);
        List<String> content = new ArrayList<>();
        content.add("自定义上报频度");
        content.add("1分钟上报一次");
        content.add("2分钟上报一次");
        content.add("3分钟上报一次");
        content.add("5分钟上报一次");
        content.add("10分钟上报一次");
        content.add("30分钟上报一次");
        content.add("60分钟上报一次");
        content.add("120钟上报一次");
        selectTimeWheel.setItems(content);
        if(key==-1){
            selectTimeWheel.setSeletion(0);
        }else{
            selectTimeWheel.setSeletion(key-1);
        }
        selectTimeWheel.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                Log.e(TAG, "onSelected: "+selectedIndex);
                if(selectedIndex==1){
                    customFreGroup.setVisibility(View.VISIBLE);
                } else {
                    customFreGroup.setVisibility(View.INVISIBLE);
                }
                currentIndex=selectedIndex;
                currentItem=item;
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    public void setStartBtn(Drawable drawable, String text){
        startBtn.setText(text);
        startBtn.setBackground(drawable);
    }

    public void initHashMap(){
        hashMap.put("自定义上报频度",30);
        hashMap.put("1分钟上报一次",60);
        hashMap.put("2分钟上报一次",120);
        hashMap.put("3分钟上报一次",180);
        hashMap.put("5分钟上报一次",300);
        hashMap.put("10分钟上报一次",600);
        hashMap.put("30分钟上报一次",1800);
        hashMap.put("60分钟上报一次",3600);
        hashMap.put("120钟上报一次",7200);
    }
    public String sendTip="下一次发送倒计时：";
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(LocationTimer locationTimer){
        nextSendTipsTV.setText(MessageFormat.format("{0}{1}{2}", sendTip, locationTimer.time,"s"));
    }
}
